package com.example.beijing.beijingdemo.fragment;

import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;

import com.example.beijing.beijingdemo.R;
import com.example.beijing.beijingdemo.activity.MainActivity;
import com.example.beijing.beijingdemo.adapter.NewsCenterTabVPAdapter;
import com.example.beijing.beijingdemo.base.BaseFragment;
import com.example.beijing.beijingdemo.base.BaseLoadNetDataOperator;
import com.example.beijing.beijingdemo.base.NewsCenterTabPager;
import com.example.beijing.beijingdemo.bean.NewsCenterBean;
import com.example.beijing.beijingdemo.util.Constants;
import com.example.beijing.beijingdemo.util.MyLogger;
import com.example.beijing.beijingdemo.util.MyToast;
import com.google.gson.Gson;
import com.viewpagerindicator.TabPageIndicator;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * 首页Home的Fragment
 */

public class NewscenterTabFragment extends BaseFragment implements BaseLoadNetDataOperator, ViewPager.OnPageChangeListener {

    // 数据bean
    private NewsCenterBean newsCenterBean;

    private TabPageIndicator mIndicator;
    private ImageButton mButtonNext;
    private ViewPager mViewPager;

    private List<NewsCenterTabPager> mViews;

    @Override
    public void intiTitle() {
        setTitle("新闻");
        setIpMenuDisplayState(true);
        setIpPicDisplayState(true);
    }

    @Override
    public View createContent() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.newscenter_content, null, false);
        mIndicator = (TabPageIndicator) view.findViewById(R.id.newscenter_content_indicator);
        mButtonNext = (ImageButton) view.findViewById(R.id.newscenter_content_next);
        mViewPager = (ViewPager) view.findViewById(R.id.newscenter_content_viewpager);

        // 点击切换到下一页
        mButtonNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int currentItem = mViewPager.getCurrentItem();
                if (currentItem != newsCenterBean.data.get(0).children.size() - 1) {
                    mViewPager.setCurrentItem(currentItem + 1);
                } else {

                }
            }
        });
        initViewPager();

        return view;
    }

    /**
     * 初始化ViewPager的数据
     */
    private void initViewPager() {
        mViews = new ArrayList<>();
        for (NewsCenterBean.NewsCenterNewsTabBean tabBean : newsCenterBean.data.get(0).children) {
            NewsCenterTabPager pageView = new NewsCenterTabPager(getContext());
            mViews.add(pageView);
        }

        // 设置适配器
        NewsCenterTabVPAdapter adapter = new NewsCenterTabVPAdapter(mViews, newsCenterBean.data.get(0).children);
        mViewPager.setAdapter(adapter);

        // 让TabIndicator和ViewPager进行联合
        mIndicator.setViewPager(mViewPager);

        // 第一个子Tab,开启自动轮播
        // TODO 这里有bug的,数据可能还没请求下来
        mViews.get(0).startSwitch();

        // 给ViewPager设置切换的监听
        // 注意: ViewPager和TabPagerIndicator配合使用,监听只能设置给TabPagerIndicator
        mIndicator.setOnPageChangeListener(this);

    }

    @Override
    public void loadNetData() {
        OkHttpUtils.get()
                .url(Constants.URL_NEWSCENTER)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        MyLogger.e("data", "数据error: 新闻中心架构");
                        MyToast.show(getActivity(), "数据error: 新闻中心架构");
                    }

                    @Override
                    public void onResponse(String response, int id) {
//                        MyToast.show(getActivity(), "有数据");
                        processData(response);
                    }
                });
    }

    /**
     * 处理数据
     */
    private void processData(String json) {
        Gson gson = new Gson();
        newsCenterBean = gson.fromJson(json, NewsCenterBean.class);

        // 把数据传递给MainActivity
        ((MainActivity) getActivity()).setData(newsCenterBean.data);

        // 创建布局
        View view = createContent();

        // 加载布局
        addView(view);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    // 新闻心中子Tab的也换
    @Override
    public void onPageSelected(int position) {
        // 当前的Tab启动自动轮播 , 其他的Tab停止切换
        for (int i = 0; i < mViews.size(); i++) {
            NewsCenterTabPager pagerView = mViews.get(i);
            if (position == i) {
                // 选中页
                pagerView.startSwitch();
            } else {
                // 未选中也
                pagerView.stopSwitch();
            }
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
}
